﻿#form
from django import forms
from django.forms import CharField, RegexField
from django.forms.widgets import *
from django.core import validators

from gmapi.forms.widgets import GoogleMap	   

class MapForm(forms.Form):
    map = forms.Field(widget=GoogleMap(attrs={'class':"map",}))


class MyAuthenticationForm(forms.Form):
    username = forms.CharField(label = ("Password"))
    password = forms.CharField(label = ("Password"), widget = forms.PasswordInput)

class RegistrationForm(forms.Form):
    username = forms.RegexField(label = ("Username"), max_length = 30, regex = r'^[\w.@+-]+$',
        help_text = ("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
        error_messages = {'invalid': ("This value may contain only letters, numbers and @/./+/-/_ characters.")})
    password = forms.CharField(label = ("Password"), widget = forms.PasswordInput)
    password_confirm = forms.CharField(label = ("Password confirmation"), widget = forms.PasswordInput,
        help_text = ("Enter the same password as above, for verification."))

    email = forms.CharField(validators = [validators.validate_email],
        error_messages = {'invalid': (u'Enter a valid e-mail address.')})

    first_name = forms.CharField(max_length = 30, required = True)
    last_name = forms.CharField(max_length = 30, required = True)
    home_zipcode = forms.CharField(required = True)
    how_user_heard_id = forms.CharField(max_length = 30, required = False)

    # check whether the username is valid or not
    def clean_username(self):
        u = self.cleaned_data['username']
        try:
            user = User.objects.get(username = u)
        except User.DoesNotExist:
            return
        raise validators.ValidationError("Username in use.")

    def clean_email(self):
        try:
            e = self.cleaned_data['email']
            validate_email(e)
            User.objects.get(email = e)
        except User.DoesNotExist:
            return
        raise validators.ValidationError("Email address in use.")

    # validate zipcode
    def clean_home_zipcode(self):
        return

    def clean_how_user_heard_id(self):
        return

    def clean_password(self):
        return

    def clean_password_confirm(self):
        return
        if self.data['password_confirm'] != self.data['password'] :
            raise validators.ValidationError('Passwords do not match.')
        return

    def save(self, field_data, type):
        # create user
        u = User.objects.create_user(field_data['username'],
                                     field_data['email'],
                                     field_data['password'])
        u.save()

        if type == "parent":
            parent = Parent(user = u,
                             type = 'parent',
                             home_zipcode = field_data['home_zipcode'],
                             first_name = field_data['first_name'],
                             last_name = field_data['last_name'])
            parent.save()

        else:
            return

        # authenticate the current user
        user = authenticate(username = field_data['username'], password = field_data['password'])
        return user